package com.zhihao.takeaway.controller;

import com.zhihao.takeaway.common.utils.EasyUITable;
import com.zhihao.takeaway.common.utils.JwtUtil;
import com.zhihao.takeaway.common.utils.Result;
import com.zhihao.takeaway.entity.Category;
import com.zhihao.takeaway.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController  {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private HttpServletRequest request;

    @GetMapping
    public Result getTableData(Integer currentPage){
        Result result = new Result();

        String EmployeeId = getEmployeeId();
        if(EmployeeId != null){
            EasyUITable easyUITable = categoryService.getTableData(currentPage);
            if(easyUITable.getTotal() != 0){
                result.success("成功获取"+easyUITable.getTotal()+"条数据");
                result.setData(easyUITable);
            }else{
                result.fail("未查询到数据");
            }
        }else{
            result.fail("当前用户未登录");
        }

        return result;
    }

    @GetMapping("/deleteById")
    public Result deleteById(String id){
        Result result = new Result();

        String EmployeeId = getEmployeeId();
        if(EmployeeId != null){
            Integer num = categoryService.deleteById(id);
            if(num == 1){
                result.success("删除成功");
            }else{
                result.fail("删除失败");
            }
        }else{
            result.fail("当前用户未登录");
        }

        return result;
    }

    @GetMapping("/search")
    public Result search(String name,Integer currentPage){
        Result result = new Result();

        String EmployeeId = getEmployeeId();
        if(EmployeeId != null){
            EasyUITable easyUITable = categoryService.search(name,currentPage);
            if(easyUITable.getTotal() != null){
                result.success("成功获取"+easyUITable.getTotal()+"条数据");
                result.setData(easyUITable);
            }else{
                result.fail("未查询到数据");
            }
        }else{
            result.fail("当前用户未登录");
        }

        return result;
    }
    @GetMapping("/getAllData")
    public Result getAllData(){
        Result result = new Result();
        String EmployeeId = getEmployeeId();
        if(EmployeeId == null || EmployeeId.equals("")){
            result.fail("你当前处于未登录状态");
        }else{
            List<Category> list = categoryService.list(null);
            result.success("获取成功");
            result.setData(new EasyUITable(list.size(),list));
        }
        return result;
    }

    @PostMapping("/updateCategory")
    public Result updateCategory(@RequestBody Category category){
        Result result = new Result();

        String EmployeeId = getEmployeeId();
        if(EmployeeId != null){
            Integer num = categoryService.updateCategory(category);
            if(num == 1){
                result.success("更新成功");
            }else{
                result.fail("更新失败");
            }
        }else{
            result.fail("当前用户未登录");
        }
        return result;
    }

    public String getEmployeeId(){
        String token = request.getHeader("token");
        String EmployeeId = JwtUtil.validateToken(token);
        return EmployeeId;
    }
}
